package com.taldh;

import org.junit.Assert;
import org.junit.Test;

public class CanJump {

    @Test
    public void canJumpTest() {
        Assert.assertTrue(canJump(new int[]{2,3,1,1,4}));
        Assert.assertFalse(canJump(new int[]{3,2,1,0,4}));
    }

    @Test
    public void canJumpTest1() {
        Assert.assertTrue(canJump(new int[]{2,0}));
    }

    @Test
    public void canJumpTest2() {
        Assert.assertTrue(canJump(new int[]{2,5,0,0}));
    }

    public boolean canJump(int[] nums) {
        if (nums == null || nums.length == 0) {
            return false;
        }
        int len = nums.length;
        int index = 0;
        while (index < len - 1) {
            int nextIndex = index + nums[index];
            if (nums[index] == 0) {
                break;
            } else {
                index = nextIndex;
            }
        }

        return index >= len - 1;
    }

}
